package com.guigu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.guigu.domain.dto.SubjectInfoDTO;
import com.guigu.domain.entity.SubjectInfo;
import com.guigu.domain.entity.SubjectMapping;
import com.guigu.domain.enums.IsDeletedFlagEnum;
import com.guigu.domain.vo.SubjectInfoVO;
import com.guigu.handler.SubjectInfoFactory;
import com.guigu.handler.SubjectInfoHandler;
import com.guigu.result.PageResult;
import com.guigu.service.ISubjectInfoService;
import com.guigu.service.ISubjectMappingService;
import com.guigu.service.SubjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @description: 题目处理
 * @author: JZC
 * @create: 2024-06-07 18:34
 **/
@Service
public class SubjectServiceImpl implements SubjectService {

    @Resource
    private SubjectInfoFactory subjectInfoFactory;
    @Resource
    private ISubjectInfoService subjectInfoService;
    @Resource
    private ISubjectMappingService subjectMappingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectInfoDTO subjectInfoDTO) {
        //1：先处理题目信息表
        //将dto转换成entity
        SubjectInfo subjectInfo = BeanUtil.toBean(subjectInfoDTO, SubjectInfo.class);
        //设置数据是否有效
        subjectInfo.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //新增数据
        subjectInfoService.save(subjectInfo);

        //2：再处理单选、多选、简单、判断表
        //获取新增的题目ID
        Long id = subjectInfo.getId();
        //设置题目ID
        subjectInfoDTO.setId(id);
        //根据题目类型去工厂调用对应的处理器
        SubjectInfoHandler subjectInfoHandler = subjectInfoFactory.getHandlers(subjectInfoDTO.getSubjectType());
        //单选、多选、简答、判断对应处理
        subjectInfoHandler.add(subjectInfoDTO);

        //3：最后处理题目映射表
        //获取所有的分类ID
        List<Integer> categoryIds = subjectInfoDTO.getCategoryIds();
        //获取所有的标签ID
        List<Integer> labelIds = subjectInfoDTO.getLabelIds();
        //定义题目映射集合
        List<SubjectMapping> subjectMappingList = new LinkedList<>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = SubjectMapping.builder().subjectId(id)
                        .isDeleted(IsDeletedFlagEnum.UN_DELETED.getCode())
                        .categoryId(Long.valueOf(categoryId))
                        .labelId(Long.valueOf(labelId)).build();
                subjectMappingList.add(subjectMapping);
            });
        });
        //批量插入映射集合
        subjectMappingService.saveBatch(subjectMappingList);
    }


    @Override
    public PageResult<SubjectInfoVO> getSubjectPage(SubjectInfoDTO subjectInfoDTO) {
        //创建分页实体对象
        PageResult pageResult = PageResult.builder()
                //请求页面大小
                .pageSize(subjectInfoDTO.getPageSize())
                //请求页码
                .pageNo(subjectInfoDTO.getPageNo()).build();
        //获查询取实体
        SubjectInfo subjectInfo = BeanUtil.toBean(subjectInfoDTO, SubjectInfo.class);
        //计算数据总记录条数
        Integer count = subjectInfoService.countByCondition(subjectInfo, subjectInfoDTO.getCategoryId(), subjectInfoDTO.getLabelId());
        if (count == 0) {
            return pageResult;
        }
        //起始页码start
        int start = (subjectInfoDTO.getPageNo() - 1) * subjectInfoDTO.getPageSize();
        //分页查询题目
        List<SubjectInfo> subjectInfos = subjectInfoService.queryPage(subjectInfo, subjectInfoDTO.getLabelId(), subjectInfoDTO.getCategoryId(), start, subjectInfoDTO.getPageSize());
        List<SubjectInfoVO> subjectInfoVOList = new LinkedList<>();
        subjectInfos.forEach(subjectInfo1 -> {
            SubjectInfoVO subjectInfoVO = BeanUtil.toBean(subjectInfo1, SubjectInfoVO.class);
            subjectInfoVOList.add(subjectInfoVO);
        });
        pageResult.setRecords(subjectInfoVOList);
        return pageResult;
    }

}
